/**
 * Licensed under the Apache License Version 2.0.
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations
 * under the License.
 */
package net.interknit.knitter.fw.types;

import javax.xml.bind.annotation.XmlRootElement;

import net.interknit.knitter.fw.CustomTypeFactory;

@XmlRootElement(name = "net.interknit.knitter.fw.types.Any")
public class Any
{
	/**
	 * The underlying state maintained by this adapter
	 */
	private java.lang.Object state = defaultState();

	/**
	 * Private method to provide the "default" state for this adapter
	 */
	private java.lang.Object defaultState()
	{
		return state != null ? state : (state = "");
	}

	/**
	 * Private method to provide the "default" value for this adapter
	 */
	private java.lang.Object defaultValue(java.lang.Object v)
	{
		return v != null ? v : "";
	}

	/**
	 * Default no-arg constructor
	 */
	public Any() { ; }

	/**
	 * Constructor to accept the user values and convert it to the underlying state.
	 * In other words, this constructor provides a way for "wrapping" the user value into this adapter.
	 * This constructor is also known as 'value constructor'
	 */
	public Any(java.lang.Object value)
	{
		this.state = defaultValue(value);
	}

	/**
	 * Adapter method to convert the underlying state back to user value.
	 * In other words, this method provides a way for "unwrapping" the user value from this adapter that
	 * was previously wrapped using the value constructor.
	 */
	public java.lang.Object unwrap()
	{
		return defaultState();
	}

	/**
	 * Return the underlying state of this adapter.
	 *
	 * @return stringified value of the underlying state
	 */
	public java.lang.String getValue()
	{
		return CustomTypeFactory.getInstance().serialize(defaultState());
	}

	/**
	 * Sets the underlying state of this adapter with the given state parameter.
	 *
	 * @param value the stringified value of the underlying state to set
	 */
	public void setValue(java.lang.String value)
	{
		this.state = CustomTypeFactory.getInstance().deserialize(value);
	}
}
